﻿using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Utility.Service
{
    /// <summary>
    /// 雪花Id服务
    /// </summary>
    public interface ISnowflakeIdService
    {
        /// <summary>
        /// 下一个Id
        /// </summary>
        /// <returns></returns>
        public long NextId();
    }

    /// <summary>
    /// Id生成服务
    /// </summary>
    public class SnowflakeIdService : ISnowflakeIdService
    {
        /// <summary>
        /// 
        /// </summary>
        public readonly long workerId;

        /// <summary>
        /// 
        /// </summary>
        public readonly long datacenterId;

        /// <summary>
        /// 
        /// </summary>
        public readonly long sequence;

        /// <summary>
        /// 
        /// </summary>
        protected readonly IdWorker worker;


        /// <summary>
        /// Id生成服务，雪花Id
        /// </summary>
        /// <param name="workerId">工作Id</param>
        /// <param name="datacenterId">数据中心Id</param>
        /// <param name="sequence">序列，默认0</param>
        public SnowflakeIdService (long workerId, long datacenterId, long sequence)
        {
            worker = new IdWorker(workerId, datacenterId);
            this.workerId = workerId;
            this.datacenterId = datacenterId;
            this.sequence = sequence;
        }

        /// <summary>
        /// 下一个Id
        /// </summary>
        /// <returns></returns>
        public long NextId()
        {
            return worker.NextId();
        }
    }
}
